package com.tdk.bts.view.manager;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.tdk.bts.action.BookInfoAction;
import com.tdk.bts.util.ImageUtil;
import com.tdk.bts.util.Page;
import com.tdk.bts.util.Request;
import com.tdk.bts.vo.BookVO;

public class BookInfoManagerView extends BaseTabbedPanel {
	
	private JTable dataTable ;
	
	private Page<BookVO> data;
	
	private BookInfoAction bookInfoAction = new BookInfoAction();
	
	public BookInfoManagerView() {
		super();
		init();
	}
	
	public void init()
	{
		removeAll();
		
		setLayout(new BorderLayout());
		
		dataTable = new JTable();
		bindData(1);
		add(new JScrollPane(dataTable));
		
		JPanel btnPanel =  new JPanel();
		
		final JLabel preLabel = new JLabel(ImageUtil.getImage("pre.png"));
		btnPanel.add(preLabel);

		final JLabel nextLabel = new JLabel(ImageUtil.getImage("next.png"));
		btnPanel.add(nextLabel);
		
		add(btnPanel,BorderLayout.SOUTH);
		
		preLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				bindData(data.getPrePage());
				if(data.getPageNo()<=1){
					preLabel.setEnabled(false);
				}
				if(data.getPageNo()<data.getTotalPage()){
					nextLabel.setEnabled(true);
				}
			}
		});
		
		nextLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				bindData(data.getNextPage());
				if(data.getPageNo()>=data.getTotalPage()){
					nextLabel.setEnabled(false);
				}
				if(data.getPageNo()>1){
					preLabel.setEnabled(true);
				}
			}
		});
	}

	public void bindData(int pageNo) {
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("电影");
		dtm.addColumn("放映时间");
		dtm.addColumn("放映厅");
		dtm.addColumn("座位");
		dtm.addColumn("状态");
		
		Request request = new Request();
		request.setParameter("pageNo", pageNo);
		try {
			bookInfoAction.findBookVOByPage(request);
			data = (Page<BookVO>) request.getAttribute("data");
			
			for (int i = 0; i < data.size(); i++) {
				BookVO vo = data.get(i);
				dtm.addRow(new Object[]{vo.getFilmName(),vo.getPublishTime(),vo.getTheaterName(),vo.getSeat(),vo.getStatus()});
			}
			
			dataTable.setModel(dtm);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
		}
	}
	
}
